//
//  YQLWelcomeViewController.swift
//  YQL_WeiBo
//
//  Created by apple on 2016/11/16.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class YQLWelcomeViewController: UIViewController {
    
    lazy var imageIcon = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    
    lazy var welcomeLabel = UILabel(title: "豆豆宝宝欢迎你!么么哒😘", textColor: UIColor.darkGray, textFont: UIFont.systemFont(ofSize: 15))
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        animationStar()
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        //界面搭建
        setupUI()
    }


    func setupUI() {
        
        self.view.addSubview(imageIcon)
        imageIcon.layer.cornerRadius = 42.5
        imageIcon.layer.masksToBounds = true
        imageIcon.sd_setImage(with: YQLOAuthViewModel.shared.userIconUrl)
        self.view.addSubview(welcomeLabel)
        welcomeLabel.alpha = 0
        
        
        imageIcon.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-120)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(imageIcon.snp.bottom).offset(20)
        }
        
    }
    
    func animationStar() {
        
        let offset = -(self.view.bounds.height - 120 - 85)
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: {
            
            self.imageIcon.snp.updateConstraints({ (make) in
                make.bottom.equalTo(self.view).offset(offset)
            })
            self.view.layoutIfNeeded()
        }) { (finish) in
            
            UIView.animate(withDuration: 0.5 , animations: {
                self.welcomeLabel.alpha = 1
            }, completion: { (finish) in
                //动画结束要跳转到tableViewController,使用通知进行修改window的rootViewController
                
                NotificationCenter.default.post(name: NSNotification.Name(KModifyRootViewControll), object: "welcomeVCPost")
                
            })
        }
        
    }
    
    
    

}
